<script lang="ts" setup>
import { PatientDelAPI, PatientMyListAPI } from '@/apis/my'
import type { PatientMyList } from '@/types/my'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'

const swipeOptions = ref([
  {
    text: '删除',
    style: {
      backgroundColor: '#dd524d',
      color: '',
      fontSize: '',
    },
  },
])
// 患者列表
const PatientList = ref<PatientMyList[]>([])
const PatientMyList = async () => {
  const res = await PatientMyListAPI()
  // console.log('res', res)
  PatientList.value = res.data
}
onLoad(() => {
  PatientMyList()
})
const DelPatient = async (id: string) => {
  // console.log(id)
  uni.showModal({
    title: '提示',
    content: '确定删除该患者吗？',
    success: async (res) => {
      if (res.confirm) {
        const res = await PatientDelAPI(id)
        uni.showToast({ title: '删除成功', icon: 'success' })
        PatientMyList()
      }
    },
  })
}
</script>

<template>
  <scroll-view>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          :right-options="swipeOptions"
          v-for="item in PatientList"
          :key="item.id"
          @click="DelPatient(item.id)"
        >
          <view class="archive-card" :class="{ active: item.defaultFlag === 1 }">
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{
                item.idCard.replace(
                  /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9Xx])$/,
                  '$1********$6'
                )
              }}</text>
              <text class="default" v-if="item.defaultFlag === 1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/pages/form/form?id=${item.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="true" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/pages/form/form">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
    </view>
  </scroll-view>
</template>

<style lang="scss">
@import './index.scss';
</style>
